package com.csglxt.view.action;

import java.math.BigDecimal;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csglxt.base.BaseAction;
import com.csglxt.domain.Notice;
import com.csglxt.domain.User;
import com.csglxt.utils.DateUtil;
import com.csglxt.utils.QueryHelper;

/**
 * 
 * @ClassName: NoticeAction
 * @Description: 公告控制器
 * @author Delver_Si
 * @date 2015-5-28 下午11:58:58
 * 
 */

@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class NoticeAction extends BaseAction<Notice> {

    private String Searchname;// 页面搜索框的值

    public String getSearchname() {
        return Searchname;
    }

    public void setSearchname(String searchname) {
        Searchname = searchname;
    }

    /**
     * 
     * 
     * @Title: list
     * @Description: TODO(返回列表视图)
     * @param
     * @return
     * @throws
     */
    public String list() {
        new QueryHelper(Notice.class, "n").addOrderProperty("date", false)
                .preparePageBean(noticeService, pageNum, pageSize);
        return "list";
    }

    /**
     * 
     * @Title: addUI
     * @Description: TODO(公告添加页面)
     * @param
     * @return 返回公告添加页面视图
     * @throws
     */
    public String addUI() {
        if (!((User) getSession().get("user")).getRole().equals("1")) {
            return "noPrivilege";
        } else {
            return "addUI";
        }
    }

    /**
     * 
     * @Title: save
     * @Description: TODO(保存公告信息)
     * @param
     * @return 保存成工后返回到公告列表
     * @throws
     */

    public String save() {
        if (!((User) getSession().get("user")).getRole().equals("1")) {
            return "noPrivilege";
        } else {
            Notice notice = new Notice();
            notice.setTitle(model.getTitle());
            notice.setContent(model.getContent());
            notice.setIsDel(new BigDecimal(1));
            notice.setDate(DateUtil.getCursqlDate());
            noticeService.save(notice);
            return "toList";
        }
    }

    /**
     * 
     * @Title: Searchlist
     * @Description: TODO(公告模糊搜索)
     * @param
     * @return 收到的结果并分页
     * @throws
     */
    public String Searchlist() {
        QueryHelper qureyHelper = new QueryHelper(Notice.class, "n")
                .addOrderProperty("date", false);
        if (Searchname != null && !Searchname.isEmpty()) {
            qureyHelper
                    .addCondition(" n.title like ? ", "%" + Searchname + "%");

        }
        qureyHelper.preparePageBean(noticeService, pageNum, pageSize);
        return "list";
    }

    /**
     * 
     * @Title: Details
     * @Description: TODO(查看公告详情)
     * @param
     * @return
     * @throws
     */
    public String Details() {
        Notice notice = noticeService.getById(model.getId());
        getValueStack().push(notice);
        return "toDetails";
    }

    /**
     * 
     * @Title: delete
     * @Description: TODO(删除公告)
     * @param
     * @return
     * @throws
     */
    public String delete() {
        if (!((User) getSession().get("user")).getRole().equals("1")) {
            return "noPrivilege";
        } else {
            noticeService.delete(model.getId());
            return "toList";
        }
    }
}
